package com.hoshiicloud.user.controller.store;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.store.StoreUsers;
import com.hoshiicloud.user.param.store.StoreUsersAddParam;
import com.hoshiicloud.user.param.store.StoreUsersQueryParam;
import com.hoshiicloud.user.param.store.StoreUsersUpdateParam;
import com.hoshiicloud.user.service.store.StoreUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 店铺用户 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-07-24
 */
@Slf4j
@RestController
@RequestMapping("/store/storeUsers")
@Api(value = "店铺用户", tags = {"店铺用户"})
@ApiSort(44)
public class StoreUsersController {
    @Autowired
    private StoreUsersService StoreUsersService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询店铺用户
     */
    @ApiOperation(value = "分页查询店铺用户")
    @PostMapping("/getPage")
    public Response page(@RequestBody StoreUsersQueryParam param) {
        try {
            QueryWrapper<StoreUsers> queryWrapper = new QueryWrapper<StoreUsers>();
            queryWrapper.lambda()
                    .eq((param.getStoreId() != null), StoreUsers::getStoreId, param.getStoreId())
                    .eq((param.getMemberId() != null), StoreUsers::getMemberId, param.getMemberId())
                    .eq(StringUtils.isNotBlank(param.getUserCode()), StoreUsers::getUserCode, param.getUserCode())
                    .eq(StringUtils.isNotBlank(param.getQrCode()), StoreUsers::getQrCode, param.getQrCode())
                    .eq(StringUtils.isNotBlank(param.getWorkTelephone()), StoreUsers::getWorkTelephone, param.getWorkTelephone())
                    .eq(StringUtils.isNotBlank(param.getPosition()), StoreUsers::getPosition, param.getPosition())
                    .eq(StringUtils.isNotBlank(param.getEducational()), StoreUsers::getEducational, param.getEducational())
                    .eq(StringUtils.isNotBlank(param.getProfessional()), StoreUsers::getProfessional, param.getProfessional())
                    .eq((param.getHireDate() != null), StoreUsers::getHireDate, param.getHireDate())
                    .eq(StringUtils.isNotBlank(param.getEmail()), StoreUsers::getEmail, param.getEmail())
                    .eq(StringUtils.isNotBlank(param.getOfficeLocation()), StoreUsers::getOfficeLocation, param.getOfficeLocation())
                    .eq(StringUtils.isNotBlank(param.getWxOpenId()), StoreUsers::getWxOpenId, param.getWxOpenId())
                    .eq(StringUtils.isNotBlank(param.getWxUnionId()), StoreUsers::getWxUnionId, param.getWxUnionId())
                    .eq(StringUtils.isNotBlank(param.getWxName()), StoreUsers::getWxName, param.getWxName())
                    .eq(StringUtils.isNotBlank(param.getWxPic()), StoreUsers::getWxPic, param.getWxPic())
                    .eq(StringUtils.isNotBlank(param.getQqOpenId()), StoreUsers::getQqOpenId, param.getQqOpenId())
                    .eq((param.getDeleted() != null), StoreUsers::getDeleted, param.getDeleted());
            IPage<StoreUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            //String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(StoreUsersService.getPage(page, queryWrapper));
        } catch (Exception e) {
            log.error("分页查询店铺用户异常", e);
            return Response.failed("分页查询店铺用户异常");
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询店铺用户
     */
    @GetMapping("/query/{id}")
    @ApiOperation(value = "根据id查询店铺用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<StoreUsers> queryById(@PathVariable("id") Long id) {
        return Response.data(StoreUsersService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺用户")
    public Response saveStoreUsers(@RequestBody StoreUsersAddParam param) {
        StoreUsers entity;
        try {
            entity = BeanUtils.covertType(param, StoreUsers.class);
        } catch (Exception e) {
            log.error("新增店铺用户Bean转换异常", e);
            return Response.failed("新增店铺用户Bean转换异常");
        }
        return Response.data(StoreUsersService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改店铺用户
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺用户")
    public Response updateStoreUsersById(@RequestBody StoreUsersUpdateParam param) {
        StoreUsers entity;
        try {
            entity = BeanUtils.covertType(param, StoreUsers.class);
        } catch (Exception e) {
            log.error("修改店铺用户Bean转换异常", e);
            return Response.failed("新增店铺用户Bean转换异常");
        }
        return Response.data(StoreUsersService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除店铺用户
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除店铺用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response deleteStoreUsersById(@PathVariable("id") Long id) {
        return Response.data(StoreUsersService.deleteStoreUsersById(id));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 禁用店铺用户
     */
    @DeleteMapping("/forbid/{id}")
    @ApiOperation(value = "禁用店铺用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response forbidStoreUsers(@PathVariable("id") Long id) {
        return Response.data(StoreUsersService.forbidStoreUsersById(id));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 禁用店铺用户
     */
    @DeleteMapping("/notForbid/{id}")
    @ApiOperation(value = "解禁店铺用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response notForbidStoreUsers(@PathVariable("id") Long id) {
        return Response.data(StoreUsersService.notForbidStoreUsersById(id));
    }

    /**
     * @param idsArr
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 批量删除店铺用户
     */
    @PostMapping("/batchDelete")
    @ApiOperation(value = "批量删除店铺用户")
    @ApiParam(name = "idsArr", value = "[1,2,3]", required = true)
    public Response batchDeleteStoreUsers(@RequestParam("idsArr") List<Long> idsArr) {
        return Response.data(StoreUsersService.batchDeleteStoreUsers(idsArr,1));
    }

    /**
     * @param idsArr
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 批量禁用店铺用户
     */
    @PostMapping("/batchForbid")
    @ApiOperation(value = "批量禁用店铺用户")
    @ApiParam(name = "idsArr", value = "[1,2,3]", required = true)
    public Response batchForbidStoreUsers(@RequestParam List<Long> idsArr) {
        return Response.data(StoreUsersService.batchDeleteStoreUsers(idsArr,2));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询店铺用户")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("获取店铺用户id集合失败");
        }
        ids.sort(null);
        return Response.data(StoreUsersService.getByIds(ids, StringUtils.join(ids, "")));
    }
}
